import { _decorator, Component, Node, Label, Button, NodeEventType } from 'cc';
import { PopupBase } from './PopupBase';
const { ccclass, property } = _decorator;

/** 
 * 对话框弹窗选项 
 */
export interface PopupMessageOptions {
    content: string; // 内容
    closeDuration?: number; // 自动关闭延时
}

/**
 * 对话框弹窗
 */
@ccclass('PopupMessage')
export class PopupMessage extends PopupBase<PopupMessageOptions> {

    // 内容文本
    @property(Label)
    public contentLabel: Label | null = null;

    protected onLoad() {
        this.registerEvent();
    }

    protected onDestroy() {
        this.unregisterEvent();
    }

    private registerEvent() {

    }

    private unregisterEvent() {

    }

    protected init() {
        this.contentLabel = this.node.getChildByPath('MessageMain/Content')?.getComponent(Label) || null;
        // 父级组件
        this.main = this.node.getChildByPath('MessageMain');
        this.background = null;
        this.blocker = this.node.getParent()?.getChildByPath('Blocker') || null;
    }

    protected updateDisplay(options: PopupMessageOptions): void {
        if (this.contentLabel) this.contentLabel.string = options.content;
        let duration = options.closeDuration || 3;
        this.scheduleOnce(() => {
            this.hide()
        }, duration);
    }

}

